package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.OrderDto;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.R;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.el.FunctionMapper;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Order order){
        log.info("提交订单信息...");

        orderService.submit(order);

        return R.success("下单成功!");
    }
    @GetMapping("/userPage")
    public R page(Integer page, Integer pageSize){
        log.info("查询订单信息");
        Page<Order> orderPage = new Page<>(page,pageSize);
        Page<OrderDto> orderDtoPage = new Page<>();
        orderService.page(orderPage);

        BeanUtils.copyProperties(orderPage,orderDtoPage,"records");

        List<Order> orderList = orderPage.getRecords();
        List<OrderDto> orderDtoList = orderList.stream().map(new Function<Order, OrderDto>() {
            @Override
            public OrderDto apply(Order order) {
                OrderDto orderDto = new OrderDto();
                BeanUtils.copyProperties(order, orderDto);
                Long orderId = order.getId();
                LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
                lqw.eq(OrderDetail::getOrderId, orderId);
                List<OrderDetail> orderDetailList = orderDetailService.list(lqw);
                orderDto.setOrderDetails(orderDetailList);
                return orderDto;
            }
        }).collect(Collectors.toList());

        orderDtoPage.setRecords(orderDtoList);

        return R.success(orderDtoPage);
    }


    @GetMapping("/page")
    public R pageOrder(Integer page, Integer pageSize, String number,
                       @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                       @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        log.info("查询订单信息...");

        Page<Order> orderPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        lqw.eq(number!=null,Order::getNumber,number);
        lqw.between((beginTime!=null&&endTime!=null),Order::getOrderTime,beginTime,endTime);

        orderService.page(orderPage,lqw);
        return R.success(orderPage);
    }

    @PutMapping
    public R updateStatus(@RequestBody Order order){
        log.info("更新订单状态....");
        orderService.updateById(order);
        return R.success("更新订单状态成功!");
    }


    @PostMapping("again")
    public R again(@RequestBody Order order){
        log.info("再来一单");
//        Order order = orderService.getById(id);
//        LambdaQueryWrapper<OrderDetail> lqwOD = new LambdaQueryWrapper<>();
//        lqwOD.eq(OrderDetail::getOrderId,id);
//        List<OrderDetail> orderDetailList = orderDetailService.list(lqwOD);
//        OrderDto orderDto = new OrderDto();
//        BeanUtils.copyProperties(order,orderDto);
//        orderDto.setOrderDetails(orderDetailList);
        return R.success("再来一单成功!");
    }
}
